#!/bin/bash

# Copyright (c) 2023 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

ROOT_DIR=`pwd`

SDK_URL="https://cidownload.openharmony.cn/version/Master_Version/OpenHarmony_5.0.0.35/20240807_020211/version-Master_Version-OpenHarmony_5.0.0.35-20240807_020211-ohos-sdk-full.tar.gz"
OHOS_SDK=ohos-sdk
SDK_SHA512SUM="9b3641fc8665e9abb33925a8ee705fbfce5182747f79707f9d08e6cf3ec0c63998d5a4defe45e12984ade638014e8cca47eb0151d27831650aff2125e6e112a6"

function prepare_sdk() {

    echo "start prepare sdk #########"

    if [ -f $OHOS_SDK.tar.gz ]
    then
        local sdk_sum=`sha512sum $OHOS_SDK.tar.gz`
        sdk_sum=${sdk_sum:0:128}
        if [ "x$sdk_sum" != "x$SDK_SHA512SUM" ]
        then
            echo "x$sdk_sum"
            echo "x$SDK_SHA512SUM"
            echo "must change SDK!!"
            rm -rf $OHOS_SDK*
        fi
    fi

    if [ ! -d $OHOS_SDK ]
    then
        if [ ! -f $OHOS_SDK.tar.gz ]
        then
            curl -f -L -- "$SDK_URL" > $OHOS_SDK.tar.gz
            if [ $? -ne 0 ]
            then
                rm -f $OHOS_SDK.tar.gz
                echo "download ohos-sdk failed! #######"
                return 1
            fi
        fi

        mkdir $OHOS_SDK
        tar -zxvf $OHOS_SDK.tar.gz -C $OHOS_SDK
        if [ $? -ne 0 ]
        then
            echo "decompress sdk package failed! #####"
            rm -rf $OHOS_SDK $OHOS_SDK.tar.gz
            return 1
        fi

        cd $OHOS_SDK/linux
        for file in `ls ./`
        do
            unzip $file
        done
        cd $ROOT_DIR
    fi

    return 0
}

cmake_package=cmake-3.26.4-linux-x86_64.tar.gz
cmake_dir=cmake-3.26.4-linux-x86_64
function prepare_cmake() {   # cmake 版本3.26
    echo "start prepare cmake #########"
    if [ ! -d $cmake_dir ]
    then
        if [ ! -f $cmake_package ]
        then
            curl -L https://cmake.org/files/v3.26/cmake-3.26.4-linux-x86_64.tar.gz --output $cmake_package
            if [ $? -ne 0 ]
            then
                rm -f $cmake_package
                echo "download cmake failed!!"
                return 1
            fi
        fi

        tar -zxf $cmake_package
        if [ $? -ne 0 ]
        then
            echo "unpack cmake failed!!!"
            rm -f $cmake_package
            return 1
        fi
    fi
    echo "cmake prepare success!!"
    return 0
}

function prepare_common_cmd() {
    local commands=("gcc" "make" "pkg-config" "autoconf" "autoreconf" "automake" "patch" "libtool" "autopoint" "gperf")
    apt update
	for cmd in ${commands[@]}
    do
        which $cmd >> /dev/null
        if [ $? -ne 0 ]
        then
            echo "install $cmd"
            apt install $cmd -y
        fi
    done
}

function prepare() {

    export PATH=/usr/local/bin:$PATH

    prepare_sdk
    if [ $? -ne 0 ]
    then
        echo "prepare_sdk failed!!"
        return 1
    fi
    prepare_cmake
    if [ $? -ne 0 ]
    then
        echo "prepare_cmake failed!!"
        return 1
    fi
    prepare_common_cmd
    echo "prepare build success!"
    return 0
}

prepare $@
exit $?

#eof
